package ru.mera.aaclog;

import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import ru.mera.aaclog.db.MessageBody;
import ru.mera.aaclog.db.MessageHeader;
import ru.mera.aaclog.db.MessageXmlBody;
import ru.mera.aaclog.dbdao.GenericDAO;

/**
 * @version 1.0 04/03/2013
 * @author anavdeev
 */
@ManagedBean
@SessionScoped
public class GetBodyBean implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    private String msg_id;
    private final EntityManagerFactory emf;
    private final EntityManager em;
    private GenericDAO<MessageBody> messageBodyDAO;
    private GenericDAO<MessageXmlBody> messageXMLBodyDAO;
    {
        emf = Persistence.createEntityManagerFactory("aaclogdb");
        em = emf.createEntityManager();
        messageBodyDAO = new GenericDAO(em);
        messageXMLBodyDAO = new GenericDAO(em);
    }

    public String getId() {
        return msg_id;
    }

    public void setId(String id) {
        this.msg_id = id;
    }
    
    public String getMessageBody() {
        String body = "";
        try {
            List<MessageBody> set = messageBodyDAO.get(MessageBody.GET_BY_MSG, new MessageHeader(new Integer(msg_id)));
            if (set.size() > 0) {
                body = set.get(0).getValue();
                //body = set.iterator().next().getValue();
            }
        } catch (Exception e) {
            Logger.getLogger(GetBodyBean.class.getName()).log(Level.SEVERE, null, e);
        }
        return body;
    }

    public String getMessageXml() {
        String body = "";
        try {
            List<MessageXmlBody> set = messageXMLBodyDAO.get(MessageXmlBody.GET_BY_MSG, new MessageHeader(new Integer(msg_id)));
            if (set.size() > 0) {
                body = set.get(0).getValue();
                //body = set.iterator().next().getValue();
            }
        } catch (Exception e) {
            Logger.getLogger(GetBodyBean.class.getName()).log(Level.SEVERE, null, e);
        }
        return body;
    }

}
